<?php
/**
 * Created by PhpStorm.
 * User: Aliaksei Lazerka
 * Date: 9/30/14
 * Time: 10:56 PM
 */

namespace parser;


if (!defined('NYMPH_ROOT')) {
  define('NYMPH_ROOT', dirname("../../../" . __FILE__));
  require(NYMPH_ROOT . '/local/loader/kernel/classes/URLClassLoader.php');
}

use files\FileUtils;

/**
 * The abstract SimpleParser class provides the main functional
 * for parsing all files
 */
abstract class SimpleParser implements IFileParser
{

  /**
   * @var array with files to parse
   * Need to be initialized with paths
   */
  private $filesToParse;

  /**
   * @var FileUtils for file actions
   * Need to be initialized with paths
   */
  protected $fileUtils;
  /**
   * @var array allowed file extensions
   * Need to be initialized with paths
   */
  protected $allowedExtensions;

  function __construct()
  {
    $this->fileUtils = new FileUtils();
  }

  abstract function init();

  /**
   * (PHP 5)<br/>
   * Adding file to some storage
   * @param string $file
   * @return boolean true if file is successfully added and false in another case
   */
  function addFileToParse($file)
  {
    $this->filesToParse[] = $file;
  }

  /**
   * (PHP 5)<br/>
   * Adding files to some storage
   * @param array $files
   * @return boolean true if file is successfully added and false in another case
   */
  function addFilesToParse($files)
  {
    $this->filesToParse = $files;
  }

  /**
   * (PHP 5)<br/>
   * Trying to parse array with objects
   * @return array array with parsed object
   */
  function parseAll()
  {
    $tmpArray = array();
    $this->init();
    foreach ($this->filesToParse as $object) {
      $tmpArray = array_merge($tmpArray, $this->parse($object));
    }
    return $tmpArray;
  }

  /**
   * (PHP 5)<br/>
   * Check file extension
   * @param array $file
   * @return boolean true if file is allowed for adding and false in another case
   */
  function checkFileExtension($file)
  {
    $fileExtension = $this->fileUtils->getFileExtension($file);
    $fileExtension = trim($fileExtension);
    if (in_array($fileExtension, $this->allowedExtensions)) {
      return true;
    } else {
      return false;
    }
  }
}